package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.website.annotion.RequireLogin;
import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
public class WebSiteController {

    @Reference
    private IUserInfoService userInfoService;



    @Reference
    private IUserInfoRedisService userInfoRedisService;
    @RequireLogin
    @RequestMapping("/hello")
    @ResponseBody
    public Object hello(Long id){
        return userInfoService.get(id);
    }
    //手机唯一性
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Object checkPhone(String phone){
      boolean c=  userInfoService.checkPhone(phone);
      return c;
    }

    //短信验证
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }
    //用户注册
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password
                            ,String rpassword    ,String verifyCode) throws LogicException{
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);

        return AjaxResult.SUCCESS;
        //判断参数是否为空

    }
  /*  @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String phone,String nickname,String password
            ,String rpassword    ,String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);

        return AjaxResult.SUCCESS;
        //判断参数是否为空*/

      /*  try{
            userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
            result =AjaxResult.SUCCESS;
        }
        catch (LogicException e){
            //给用户看的
        }
        catch (Exception e){
            //系统的
            e.printStackTrace();
            result = new AjaxResult(false,"系统繁忙,请稍后");
        }*/
//    }


    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(HttpServletRequest request, HttpServletResponse response,String username
                                ,String password                  ){
        String token = userInfoService.userLogin(username,password);
      /* Cookie c = new Cookie("token",token);
        c.setMaxAge(RedisKeys.USER_LOGIN_TOKEN.getTime().intValue());//30min
        c.setPath("/");
        response.addCookie(c);*/
        CookieUtil.addCookie("token",token,RedisKeys.USER_LOGIN_TOKEN.getTime().intValue(),response);
        return AjaxResult.SUCCESS;
    }


}
